function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { FullscreenExitOutlined, FullscreenOutlined } from '@ant-design/icons';
import React, { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { iup, useUpdateEffect } from "../../util";
import "./index.less";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var fullStyleInBody = {
  position: 'fixed',
  width: '100%',
  height: '100%',
  top: 0,
  bottom: 0,
  left: 0,
  right: 0
};
function InnerPage(_ref, ref) {
  var children = _ref.children,
    className = _ref.className,
    _ref$iconStyle = _ref.iconStyle,
    iconStyle = _ref$iconStyle === void 0 ? {} : _ref$iconStyle,
    _ref$style = _ref.style,
    style = _ref$style === void 0 ? {} : _ref$style,
    _ref$showIcon = _ref.showIcon,
    showIcon = _ref$showIcon === void 0 ? true : _ref$showIcon,
    _ref$residentIcon = _ref.residentIcon,
    residentIcon = _ref$residentIcon === void 0 ? false : _ref$residentIcon,
    _ref$innerStyle = _ref.innerStyle,
    innerStyle = _ref$innerStyle === void 0 ? {} : _ref$innerStyle,
    getIconContainer = _ref.getIconContainer,
    getFullContainer = _ref.getFullContainer,
    _ref$zIndex = _ref.zIndex,
    zIndex = _ref$zIndex === void 0 ? 99 : _ref$zIndex,
    onFullScreen = _ref.onFullScreen;
  var el = useRef();
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isFullPage = _useState2[0],
    setIsFullPage = _useState2[1];
  var _innerStyle = _objectSpread({
    width: '100%',
    height: '100%'
  }, innerStyle);
  var _useState3 = useState({}),
    _useState4 = _slicedToArray(_useState3, 2),
    fullStyle = _useState4[0],
    setFullStyle = _useState4[1];
  var fullHandle = function fullHandle(newIsFullPage) {
    if (newIsFullPage) {
      var _el$current, _el$current$getBoundi;
      var _ref2 = ((_el$current = el.current) === null || _el$current === void 0 || (_el$current$getBoundi = _el$current.getBoundingClientRect) === null || _el$current$getBoundi === void 0 ? void 0 : _el$current$getBoundi.call(_el$current)) || {},
        top = _ref2.top,
        left = _ref2.left,
        right = _ref2.right,
        bottom = _ref2.bottom;
      if (top !== undefined) {
        var mw = document.documentElement.clientWidth;
        var mh = document.documentElement.clientHeight;
        el.current.parentStyle = {
          width: el.current.parentElement.offsetWidth,
          height: el.current.parentElement.offsetHeight
        };
        el.current.parentElement.style.width = el.current.parentStyle.width + 'px';
        el.current.parentElement.style.height = el.current.parentStyle.height + 'px';
        el.current.style.position = 'fixed';
        el.current.style.top = top + 'px';
        el.current.style.left = left + 'px';
        el.current.style.right = mw - right + 'px';
        el.current.style.bottom = mh - bottom + 'px';
        el.current.style.width = 'auto';
        el.current.style.height = 'auto';
        setTimeout(function () {
          setIsFullPage(true);
        });
        return;
      }
    }
    el.current.style.width = iup.isNumber(_innerStyle.width) ? _innerStyle.width + 'px' : _innerStyle.width;
    el.current.style.height = iup.isNumber(_innerStyle.height) ? _innerStyle.height + 'px' : _innerStyle.height;
    setIsFullPage(newIsFullPage);
  };
  useUpdateEffect(function () {
    if (isFullPage) {
      var escEvent = function escEvent(e) {
        if (e.keyCode === 27) {
          setIsFullPage(false);
        }
      };
      var newStyle = fullStyleInBody;
      var io;
      var container = getFullContainer === null || getFullContainer === void 0 ? void 0 : getFullContainer();
      if (!container) {
        var _iup$external$getQian, _iup$external;
        var name = (_iup$external$getQian = (_iup$external = iup.external).getQianKun) === null || _iup$external$getQian === void 0 ? void 0 : _iup$external$getQian.call(_iup$external).name;
        container = name ? document.body.querySelector("div[data-qiankun=\"".concat(name, "\"]")) : null;
      }
      if (container) {
        el.current.targetContainer = container;
        el.current.lastDisplay = el.current.style.display;
        var domRect = container.getBoundingClientRect();
        var mw = document.documentElement.clientWidth;
        var mh = document.documentElement.clientHeight;
        newStyle = _objectSpread(_objectSpread({}, fullStyleInBody), {}, {
          left: domRect.left,
          top: domRect.top,
          right: mw - domRect.right,
          bottom: mh - domRect.bottom
        });
        if (window.IntersectionObserver) {
          var _io = new IntersectionObserver(function (entries) {
            if (el.current) {
              if (entries[0].isIntersecting) {
                el.current.style.display = el.current.lastDisplay;
              } else {
                el.current.style.display = 'none';
              }
            }
          }, {
            root: el.current.targetContainer.parentElement,
            threshold: [0, 1]
          });
          _io.observe(el.current.targetContainer);
        }
      } else {
        el.current.targetContainer = document.body;
      }
      el.current.lastOverFlow = el.current.targetContainer.style.overflow;
      el.current.targetContainer.style.overflow = 'hidden';
      document.addEventListener('keydown', escEvent);
      setFullStyle(newStyle);
      return function () {
        document.removeEventListener('keydown', escEvent);
        if (io) {
          io.disconnect();
          io = null;
        }
      };
    } else {
      el.current.targetContainer.style.overflow = el.current.lastOverFlow;
    }
    onFullScreen === null || onFullScreen === void 0 || onFullScreen(isFullPage);
  }, [isFullPage]);
  useImperativeHandle(ref, function () {
    return {
      setFullPage: fullHandle
    };
  });
  return /*#__PURE__*/_jsx("div", {
    className: className,
    style: isFullPage ? _objectSpread(_objectSpread({}, style), el.current.parentStyle) : _objectSpread({
      display: 'inline-block',
      width: '100%'
    }, style),
    children: /*#__PURE__*/_jsxs("div", {
      className: iup.classNames('iup-full-page', {
        'resident-icon': showIcon && (residentIcon || isFullPage)
      }),
      ref: el,
      style: isFullPage ? _objectSpread(_objectSpread({
        zIndex: zIndex
      }, _innerStyle), fullStyle) : _innerStyle,
      children: [/*#__PURE__*/_jsx(Icon, {
        getIconContainer: getIconContainer,
        showIcon: showIcon,
        onClick: function onClick() {
          return fullHandle(!isFullPage);
        },
        iconStyle: iconStyle,
        isFullPage: isFullPage
      }), children]
    })
  });
}
function Icon(_ref3) {
  var getIconContainer = _ref3.getIconContainer,
    showIcon = _ref3.showIcon,
    onClick = _ref3.onClick,
    isFullPage = _ref3.isFullPage,
    iconStyle = _ref3.iconStyle;
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    mounted = _useState6[0],
    setMounted = _useState6[1];
  useEffect(function () {
    setMounted(true);
  }, []);
  var targetEl = useMemo(function () {
    var iconContaner = iup.isFunction(getIconContainer) ? getIconContainer() : null;
    if (iconContaner && !iconContaner.style.position) {
      iconContaner.style.position = 'relative';
    }
    return iconContaner;
  }, [mounted]);
  if (mounted) {
    var getChildren = function getChildren(style) {
      return showIcon && /*#__PURE__*/_jsx("span", {
        title: isFullPage ? '退出全屏' : '全屏',
        onClick: onClick,
        className: "btn-icon",
        style: _objectSpread(_objectSpread({
          fontSize: 18,
          lineHeight: 0
        }, style), iconStyle),
        children: isFullPage ? /*#__PURE__*/_jsx(FullscreenExitOutlined, {}) : /*#__PURE__*/_jsx(FullscreenOutlined, {})
      });
    };
    return targetEl ? /*#__PURE__*/ReactDOM.createPortal(getChildren({
      top: '50%',
      right: 5,
      transform: 'translateY(-50%)'
    }), targetEl) : getChildren({
      top: 12,
      right: 12
    });
  }
  return null;
}
export var FullPage = /*#__PURE__*/React.forwardRef(InnerPage);